﻿using ChessTest.Chess;
using org.xisoft.game.GobangModule;
using ReversiModule;
using System;
using System.IO;
using System.Windows.Forms;

namespace GameStart
{
    public partial class MainForm : Form
    {
    
        public MainForm()
        {
            InitializeComponent();
        }


        private void OnSaveClick(object sender, EventArgs e)
        {
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                saveFileDialog.Filter = "棋盘文件|*.broad";//设置文件类型

                saveFileDialog.DefaultExt = "broad";//设置默认格式（可以不设）
                saveFileDialog.AddExtension = true;//设置自动在文件名中添加扩展名
                saveFileDialog.InitialDirectory = Environment.CurrentDirectory +"/saved";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    Save(saveFileDialog.FileName);
            }

        }

        public void Save(string fileName)
        {
            using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate))
            {
                BinaryWriter bw = new BinaryWriter(fs);
                chessBoardControl1.Save(bw);
                bw.Close();
            }
        }



        public void Read(String path)
        {
            using (FileStream fs = File.OpenRead(path))
            {
                BinaryReader br = new BinaryReader(fs);
                chessBoardControl1.Read(br);
                br.Close();
            }
        }

        private void OnReadClick(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "棋盘文件|*.broad";
                openFileDialog.RestoreDirectory = false;    //若为false，则打开对话框后为上次的目录。若为true，则为初始目录
                openFileDialog.InitialDirectory = Environment.CurrentDirectory + "/saved";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                    Read(openFileDialog.FileName);
            }
        }

        private void 五子棋ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Type type = typeof(GobangBroadControl);
            chessBoardControl1.Init(type.AssemblyQualifiedName);
        }

        private void 中国象棋ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Type type = typeof(ChessboardPanel);
            chessBoardControl1.Init(type.AssemblyQualifiedName);
        }

        private void 黑白棋ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Type type = typeof(ReversiBoardPanel);
            chessBoardControl1.Init(type.AssemblyQualifiedName);
        }
    }
}
